# import json
# import logging
# from datetime import datetime
#
# from django.shortcuts import render
# from django.utils.decorators import method_decorator
# from django.views.decorators.csrf import csrf_exempt
# from django.utils import timezone
# from rest_framework.permissions import IsAuthenticated
# from rest_framework.response import Response
# from rest_framework.views import APIView
#
# from orders.models import Order
# from payment.models import PaymentRecord
# from payment.wxpay import WxPayClient
# from utils.snowflake_singleton import SnowflakeSingleton
#
# logger = logging.getLogger('django')
#
#
# # Create your views here.
#
# class WxPayOrderApiView(APIView):
#     permission_classes = [IsAuthenticated]
#
#     def post(self, request):
#         """
#         微信下单,对接微信Native下单接口
#         """
#         # 接受参数
#         order_number = request.data.get('order_number')
#         # 校验参数
#         if not order_number:
#             return Response({"code": 0, "message": "订单编号不能为空"})
#
#         try:
#             # 获取订单
#             order = Order.objects.get(order_number=order_number, user=request.user)
#             # 校验订单状态
#             if order.order_status != 0:
#                 return Response({"code": 0, "message": "订单状态有误"})
#
#             snowflake = SnowflakeSingleton.get_instance(worker_id=2)
#
#             # 生成支付订单号
#             pay_order_number = str(snowflake.get_id())
#
#             # 创建支付记录
#             payment_record = PaymentRecord.objects.create(
#                 order=order,
#                 order_number=order_number,
#                 pay_order_number=pay_order_number,
#                 trade_no="",
#                 pay_method=2,
#                 pay_amount=order.real_price,
#                 pay_status=0,
#
#             )
#
#             # 调用微信支付
#             client = WxPayClient()
#             total_fee = int(float(order.real_price) * 100)  # 单位是分
#             description = f"码境空间课程订单:{order_number}"
#             code_url = client.create_order(pay_order_number, total_fee, description)
#             if not code_url:
#                 return Response({"code": 0, "message": "微信下单失败"})
#                 # 更新支付记录
#             payment_record.pay_remark = "发起微信支付成功"
#             payment_record.save()
#             # 更新订单:订单支付方式
#             order.pay_method = 2
#             order.save()
#
#             # 构建返回结果
#             result = {
#                 'pay_order_number': pay_order_number,
#                 'pay_url': code_url,
#                 'total_fee': total_fee,
#                 'order_number': order_number,
#             }
#
#             return Response({"code": 1, "message": "微信下单成功", "data": result})
#
#         except Exception as e:
#             logger.error(f"微信下单异常：{str(e)}")
#             return Response({"code": 0, "message": str(e)})
#
#
# @method_decorator(csrf_exempt, name='dispatch')
# class WxPayNotifyApiView(APIView):
#     """
#     微信支付结果通知回调接口
#     """
#     # 不需要身份验证，因为是微信服务器发起的回调请求
#     authentication_classes = []
#     permission_classes = []
#
#     def post(self, request):
#         try:
#             body_bytes = request.body or b''
#             payload = json.loads(body_bytes.decode('utf-8')) if body_bytes else {}
#
#             event_type = payload.get('event_type')
#             resource = payload.get('resource') or {}
#             logger.info(f'微信回调资源体: {resource}')
#
#             if not resource:
#                 logger.error('微信回调资源体为空')
#                 return Response({'code': 'FAIL', 'message': 'Bad Request'}, status=400)
#
#             client = WxPayClient()
#             # 可选：如启用平台证书，可先验签
#             if not client.verify_callback_signature(request.headers, body_bytes.decode('utf-8')):
#                 logger.error('微信回调验签失败')
#                 return Response({'code': 'FAIL', 'message': 'Signature Verify Fail'}, status=401)
#
#             try:
#                 decrypted = client.decrypt_notification(resource)
#             except Exception as decrypt_err:
#                 logger.error(f'微信回调解密失败: {decrypt_err}')
#                 return Response({'code': 'FAIL', 'message': 'Decrypt Fail'}, status=400)
#
#             # 解密后的交易数据（参考官方文档）
#             out_trade_no = decrypted.get('out_trade_no')
#             transaction_id = decrypted.get('transaction_id')
#             trade_state = decrypted.get('trade_state')
#             success_time_str = decrypted.get('success_time')
#             amount_info = decrypted.get('amount') or {}
#             payer_info = decrypted.get('payer') or {}
#
#             # 非成功事件可直接忽略返回成功（避免微信重试）
#             if trade_state != 'SUCCESS' or event_type != 'TRANSACTION.SUCCESS':
#                 logger.info(
#                     f'非成功回调，已忽略: trade_state={trade_state}, event_type={event_type}, out_trade_no={out_trade_no}')
#                 return Response({'code': 'SUCCESS', 'message': '成功'})
#
#             # 幂等：若记录已成功处理，则直接返回
#             payment_record = PaymentRecord.objects.filter(pay_order_number=out_trade_no).order_by('-id').first()
#             if not payment_record:
#                 logger.error(f'未找到支付记录: out_trade_no={out_trade_no}')
#                 return Response({'code': 'FAIL', 'message': 'Payment Record Not Found'}, status=404)
#
#             if payment_record.pay_status == 1 and payment_record.trade_no:
#                 logger.info(f'重复回调，已处理过: out_trade_no={out_trade_no}')
#                 return Response({'code': 'SUCCESS', 'message': '成功'})
#
#             # 订单金额校验（单位：分）
#             total_paid_cents = int(amount_info.get('total') or 0)
#             expected_cents = int(float(payment_record.pay_amount) * 100)
#             if total_paid_cents != expected_cents:
#                 logger.error(
#                     f'金额不匹配: notify={total_paid_cents}, expected={expected_cents}, out_trade_no={out_trade_no}')
#                 return Response({'code': 'FAIL', 'message': 'Amount Mismatch'}, status=400)
#
#             # 更新支付记录
#             payment_record.pay_status = 1
#             payment_record.trade_no = transaction_id
#             try:
#                 if success_time_str:
#                     # 例如: 2020-12-31T15:01:02+08:00 -> 转为本地时区的“无时区”时间（兼容 USE_TZ=False）
#                     parsed_dt = datetime.fromisoformat(success_time_str)
#                     if timezone.is_aware(parsed_dt):
#                         local_dt = parsed_dt.astimezone(timezone.get_current_timezone())
#                         payment_record.pay_time = local_dt.replace(tzinfo=None)
#                     else:
#                         payment_record.pay_time = parsed_dt
#                 else:
#                     payment_record.pay_time = timezone.now()
#             except Exception:
#                 payment_record.pay_time = timezone.now()
#             payment_record.pay_remark = '微信支付成功'
#             payment_record.save()
#
#             # 同步更新订单
#             order = payment_record.order
#             order.order_status = 1
#             order.trade_no = transaction_id
#             try:
#                 if success_time_str:
#                     parsed_dt = datetime.fromisoformat(success_time_str)
#                     if timezone.is_aware(parsed_dt):
#                         local_dt = parsed_dt.astimezone(timezone.get_current_timezone())
#                         order.pay_time = local_dt.replace(tzinfo=None)
#                     else:
#                         order.pay_time = parsed_dt
#                 else:
#                     order.pay_time = timezone.now()
#             except Exception:
#                 order.pay_time = timezone.now()
#             order.save()
#
#             return Response({'code': 'SUCCESS', 'message': '成功'})
#
#         except Exception as e:
#             logger.error(f'微信回调处理异常: {e}')
#             return Response({'code': 'FAIL', 'message': 'Internal Error'}, status=500)
#
#
# def parse_time(time_str):
#     """处理微信支付返回的时间字符串"""
#     if not time_str:
#         return None
#     # 将ISO格式时间字符串转换为datetime对象
#     try:
#         # 处理带时区的时间字符串，去除时区信息
#         time_str = time_str.split('+')[0].replace('T', ' ')
#         return datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
#     except Exception as e:
#         logger.error(f"时间解析异常：{str(e)}")
#         return None
#
#
# class WxPayQueryAPIView(APIView):
#     permission_classes = [IsAuthenticated]
#
#     def get(self, request):
#         """
#         微信支付订单查询
#         """
#         # 接受参数
#         order_number = request.query_params.get('order_number')
#         pay_order_number = request.query_params.get('pay_order_number')
#         # 校验参数
#         if not order_number:
#             return Response({"code": 0, "message": "订单号不能为空"})
#
#         try:
#             order = Order.objects.get(order_number=order_number, user=request.user)
#             if order.order_status == 1:
#                 return Response({"code": 1, "message": "订单已支付", "order_number": order_number})
#
#             # 查询支付记录
#             try:
#                 payment_record = PaymentRecord.objects.get(order_number=order_number, pay_order_number=pay_order_number,
#                                                            pay_method=2)
#             except PaymentRecord.DoesNotExist:
#                 return Response({"code": 0, "message": "支付记录不存在", 'order_number': order_number})
#
#             client = WxPayClient()
#             result = client.query_order(pay_order_number)
#             trade_state = result.get("trade_state")
#             success_time = parse_time(result.get("success_time"))
#             transaction_id = result.get("transaction_id")
#             logger.info(f"微信支付查询结果:{result}")
#             logger.info(f"微信支付查询结果:{trade_state},{success_time},{transaction_id}")
#             if trade_state == "SUCCESS":
#
#                 logger.info(f"订单{order_number}支付成功")
#                 # 更新订单状态
#                 order.order_status = 1
#                 order.pay_time = success_time
#                 order.trade_no = transaction_id
#                 order.save()
#
#                 payment_record.pay_status = 1
#                 payment_record.pay_time = success_time
#                 payment_record.trade_no = transaction_id
#                 payment_record.pay_remark = "微信支付成功"
#                 payment_record.save()
#
#                 return Response({
#                     "code": 1,
#                     "message": "订单支付成功",
#                     "order_number": order_number
#                 })
#
#             else:
#                 logger.info(f"订单{order_number}支付失败")
#                 return Response({
#                     "code": 0,
#                     "message": "订单支付失败",
#                     "order_number": order_number
#                 })
#         except Exception as e:
#             logger.error(f"微信支付查询异常：{str(e)}")
#             return Response({"code": 0, "message": f"查询支付状态失败:{str(e)}"})
